﻿using Benefactor.Models.DtoModels;
using Benefactor.Services;
using Benefactor.Services.Repositories;
using Bigway.Common.Models.DtoModels;
using Microsoft.AspNetCore.Mvc;

namespace Benefactor.Controllers;
using static Errors;

[ApiController]
[Route("api/product_type")]
public class ProductTypeController(
    IProductTypeRepository productTypeRepository,
    IAppConfig appConfig,
    ILogger<ProductTypeController> logger) : ControllerBase
{
    [HttpGet]
    public async Task<GetProductTypeResponseDto> GetProductTypesAsync(
        [FromQuery(Name = "api_key")] string apiKey,
        [FromQuery(Name = "system_id")] string systemId,
        CancellationToken token = default
    )
    {
        logger.LogInformation("Getting product types of system {SystemId}", systemId);
        if (apiKey != appConfig.ApiKey)
        {
            return Unauthorized<GetProductTypeResponseDto>();
        }
        var productTypes = await productTypeRepository.GetAllAsync(systemId, null, token);
        return new GetProductTypeResponseDto
        {
            Data = productTypes.OrderBy(x => x.Order)
        };
    }
}